<?php

namespace Api\Controller;
use Think\Controller;

class PublicController extends Controller
{

    //公众号主页
    public function index(){
        $wx = new \Org\Wx\Wxapi;
        $config = C('WECHAT_CONF');
        $code = I('get.code');
        //oauth2授权获取用户资料
        if(!empty($code)){
            //获取access_token
            $access_token_arr = $wx -> get_access_token($config['appid'],$config['appsecret'],$code);
            // 刷新access_token
            if(empty($access_token_arr['access_token'])){
                $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=".$config['appid']."&grant_type=refresh_token&refresh_token=".$access_token_arr['refresh_token'];
                $access_token_arr=$wx -> request_get($url);
            }
            $userinfo = $wx -> get_wx_userinfo($access_token_arr['access_token'],$access_token_arr['openid']);
            $map['wx_openid'] = $userinfo['openid'];
            if(!empty($userinfo['openid'])){
                $data_b = M('agent')-> where($map) -> find();
                if(!empty($data_b)){
                      $userinfo['uid']=$data_b['uid'];
                      $userinfo['username']=$data_b['username'];
                      session ( 'user_auth', $userinfo );
                      $this->redirect('Index/index');
                }else{
                    session ( 'user_auth', $userinfo );
                    $this->redirect('Index/index');
                }

            }

        }else{
            echo "错误";
            die();
        }
    }



    //修改资料
    public function editInfo(){
      if (IS_POST) {
            $data=I('post.');
            $uid=is_wxlogin(); 
            $res= M('agent')->where(array('uid'=>$uid))->save($data);
            if($res!==false){ //
                $this->success('设置成功！',U('Agent/profile'));
            } else {
                $this->error("设置错误",'', true);
            }
        }else{
            $uid=is_wxlogin();
            $info=M('agent')->where('uid='.$uid)->find();
            $this->assign('info',$info);
            $this->display();
        }
    }

    public function bindUser(){
         $userinfo=session ( 'user_auth');
         if (IS_POST) {
            $data['username']=I('post.username');
            $data['password']=I('post.password');   
            $data['wx_openid']=$userinfo['openid'];     
            $re = D('Agent')->login($data['username'],$data['password']);
            if(0 < $re){ //注册成功
                $this->success('绑定成功！',U('Agent/profile'));
            } else { //注册失败，显示错误信息
                //用户不存在
                if($status==-1){
                     $uid = D('Agent')->register($data);
                     if(0 < $uid){ //注册成功
                         $this->success('绑定成功！',U('Agent/profile'));
                     } else { //注册失败，显示错误信息
                         $this->error($this->showRegError($uid),'', true);
                     }
                }else{
                    $this->error("密码错误",'', true);
                }
            }
        }else{
             
            $map['wx_openid']=$userinfo['openid'];  
            $re=M('agent')->where($map)->find();
            if(!empty($re)){
                $this->redirect('Public/editInfo');
            }
            $this->display();
        }
    }




    /**
     * 检验表单字段
     */
    public function checkfield(){
        if(!UID){
            $this->error('请先登录',U('login'));
        }
        $where['ID_number']=I('post.ID_number');
        $where['type']=I('post.type');
        $data=M('student_info')->where($where)->find();
        if(is_array($data)){
            $this->ajaxReturn(['status'=>1,"info"=>'身份证号已存在']);
        }else{
            $this->ajaxReturn(['status'=>0]);
        }
    }



   public function introduction(){
       $info  =   M("Config")->where(array('name'=>'WEB_SERVICE_PROTCOL'))->field('id,name,title,extra,value,remark,type')->find();
        $this->assign('info',$info);
       $this->display();
   }

    public function level(){
       $info  =   M("Config")->where(array('name'=>'WEB_LEVEL'))->field('id,name,title,extra,value,remark,type')->find();
        $this->assign('info',$info);
       $this->display();
   }



   /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    public function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须时6位的数字加字符格式！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }
}